class ScoresController < UserController
  before_action :set_score, only: [:show, :edit, :update, :destroy]

  # GET /scores
  # GET /scores.json
  def index
    #if current_user.department.level == 1
    #  @scores = Score.joins(:department).all
    #elsif current_user.department.level == 2
    #  @scores = Score.joins(:department).where('departments.master_id = ?', current_user.department.id)
    #else
    #  @scores = Score.joins(:department).where(user_id: current_user.id)
    #end
    @q = Score.by_user(current_user).ransack(params[:q])
    @scores = @q.result.includes(:department, :bus_type, :term_type)
  end

  # GET /scores/1
  # GET /scores/1.json
  def show
  end

  # GET /scores/new
  def new
    @score = Score.new
  end

  # GET /scores/1/edit
  def edit
  end

  # POST /scores
  # POST /scores.json
  def create
    @score = Score.new(score_params)
    @score.user_id = current_user.id
    @score.department_id = current_user.department.id
    @score.master_id = current_user.department.master_id

    crecord = CoreRecord.find_by(department_id: current_user.department_id, create_date: @score.freeze_date, stan: @score.stan)

    respond_to do |format|
      if crecord.nil?
        @score.errors.add(:stan, "流水号不存在")
        format.html { render :new }
        format.json { render json: @score.errors, status: :unprocessable_entity }
      else
        @score.acct_name = crecord.cust_name
        @score.id_type_id = crecord.crdtype
        @score.id_no = crecord.crdid
        @score.amount = crecord.amount
        @score.day_num = crecord.days
        @score.create_date = crecord.start_date
        @score.acct_no = crecord.account
        @score.stan = crecord.stan
        @score.phone = crecord.mobile
        @score.acct_type_id = crecord.acc_type
        @score.set_score
        if @score.save
          format.html { redirect_to @score, notice: 'Score was successfully created.' }
          format.json { render :show, status: :created, location: @score }
        else
          format.html { render :new }
          format.json { render json: @score.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  # PATCH/PUT /scores/1
  # PATCH/PUT /scores/1.json
  def update
    respond_to do |format|
      if @score.update(score_params)
        format.html { redirect_to @score, notice: 'Score was successfully updated.' }
        format.json { render :show, status: :ok, location: @score }
      else
        format.html { render :edit }
        format.json { render json: @score.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /scores/1
  # DELETE /scores/1.json
  def destroy
    @score.destroy
    respond_to do |format|
      format.html { redirect_to scores_url, notice: 'Score was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_score
      @score = Score.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def score_params
      params.require(:score).permit(:user_id, :department_id, :acct_name, :id_type_id, :id_no, :phone, :bus_type_id, :acct_type_id, :acct_no, :create_date, :amount, :term_type_id, :score, :stan, :freeze_date, :desc)
    end

    def get_score
      if @score.term_type == 2
        @score.amount / 10000 * 60
      elsif @score.term_type == 1
        @score.amount / 10000 * 30
      else
        @score.amount / 10000 * 15
      end
    end
end
